package com.wx.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;
import org.springframework.web.servlet.config.annotation.*;

import java.nio.charset.Charset;
import java.util.Arrays;
import java.util.List;

/**
 * WEB MVC 配置
 *
 * @author wx
 */
@Configuration
public class WebConfAdapter implements WebMvcConfigurer  {


  @Autowired
  private MyUserAuthenticationInterceptor securityInterceptor;
  /**
   *
   * @param registry
   */
  @Override
  public void addInterceptors(InterceptorRegistry registry) {
    //后台登录拦截器拦截路径
    registry.addInterceptor(securityInterceptor)
            .addPathPatterns("/**")// 拦截 url http://127.0.0.1:8888/admin/***
            .excludePathPatterns("/login.do")
            .excludePathPatterns("/css/**")
            .excludePathPatterns("/images/**")
            .excludePathPatterns("/admin"); // 不拦截 登陆页面不用拦截

  }
  /**
   * 设置欢迎
   * @param registry
   */
  @Override
  public void addViewControllers(ViewControllerRegistry registry) {
    //主页
    registry.addViewController("/").setViewName("forward:/admin");
  }

  @Override
  public void addResourceHandlers(ResourceHandlerRegistry registry) {
    //配置静态资源路径
    registry.addResourceHandler("/**").addResourceLocations("classpath:static/");
  }




//  @Bean
//  public CorsFilter corsFilter() {
//    UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
//    CorsConfiguration configuration = new CorsConfiguration();
//    configuration.setAllowCredentials(true);
//    configuration.addAllowedOrigin("*");
//    configuration.addAllowedHeader("*");
//    configuration.setAllowedMethods(Arrays.asList("GET", "PUT", "POST", "DELETE"));
//    source.registerCorsConfiguration("/**", configuration);
//    return new CorsFilter(source);
//  }
//
//  @Bean
//  public HttpMessageConverter<String> responseBodyConverter() {
//    //编码
//    return new StringHttpMessageConverter(Charset.forName("UTF-8"));
//  }

}
